package com.googlecode.allinclusive.fw.core.utils;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;

import com.googlecode.allinclusive.fw.core.springframework.utils.SpringUtils;

public class XMLUtils {
	
	// dependence spring oxm
	public String writeToXML(Object obj) throws XmlMappingException, IOException {
		Marshaller marshaller = SpringUtils.getBean(Marshaller.class);
		StringWriter sw = new StringWriter();
		marshaller.marshal(obj, new StreamResult(sw));
		return sw.getBuffer().toString();
	}
	
	public Object writeToObject(String strXML) throws XmlMappingException, IOException {
		Unmarshaller unmarshaller = SpringUtils.getBean(Unmarshaller.class);
		StringReader sr = new StringReader(strXML);
		Object obj = unmarshaller.unmarshal(new StreamSource(sr));
		return obj;
	}
}
